package chapter4;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author caiya
 * @date 2020/3/25 22:21
 *
 * 独占锁：ReentrantLock
 */
public class ReentrantLockList {
    private ArrayList<String> arrayList = new ArrayList<>();
    private volatile ReentrantLock lock = new ReentrantLock();

    /**
     * 添加
     * @param e
     */
    public void add(String e) {
        lock.lock();
        try {
            arrayList.add(e);
        }finally {
            lock.unlock();
        }
    }

    /**
     * 删除
     * @param e
     */
    public void remove(String e) {
        lock.lock();
        try {
            arrayList.remove(e);
        }finally {
            lock.unlock();
        }
    }

    /**
     * 查询
     * @param index
     * @return
     */
    public String get(int index) {
        lock.lock();
        try {
            return arrayList.get(index);
        }finally {
            lock.unlock();
        }
    }

}
